from odoo import fields, models, api


class UpdbsDataModelLine(models.Model):
    _name = 'updbs.model.line'
    _description = 'Updbs Model Fields Data'
    _rec_name = 'fields_id'

    updbs_model_id = fields.Many2one('updbs.model', string='Updbs Model', track_visibility='onchange')
    fields_id = fields.Many2one('ir.model.fields', string='Model Fields', required=True, track_visibility='onchange')
    field_description = fields.Char(related='fields_id.field_description', string='Field Type', track_visibility='onchange')
    fields_type = fields.Selection(related='fields_id.ttype', string='Field Type', track_visibility='onchange')
    required = fields.Boolean(related='fields_id.required', string='Required', track_visibility='onchange')
    readonly = fields.Boolean(related='fields_id.readonly', string='Readonly', track_visibility='onchange')
    index = fields.Boolean(related='fields_id.index', string='Indexed', track_visibility='onchange')
    state = fields.Selection(related='fields_id.state', string='Type', track_visibility='onchange')
    store = fields.Boolean(related='fields_id.store', string='Stored', track_visibility='onchange')

    ov_Boolean = fields.Boolean(string='Original Value', readonly=True, store=True)
    ov_Integer = fields.Integer(string='Original Value', readonly=True, store=True)
    ov_Float = fields.Float(string='Original Value', readonly=True, store=True)
    # ov_Monetary = fields.Monetary(string='Original Value', readonly=True, store=True)
    ov_Char = fields.Char(string='Original Value', readonly=True, store=True)
    ov_Text = fields.Text(string='Original Value', readonly=True, store=True)
    ov_Html = fields.Html(string='Original Value', readonly=True, store=True)
    ov_Date = fields.Date(string='Original Value', readonly=True, store=True)
    ov_Datetime = fields.Datetime(string='Original Value', readonly=True, store=True)
    ov_Binary = fields.Binary(string='Original Value', readonly=True, store=True)

    # ov_Selection = fields.Selection(selection=[('','')], string='Original Value', readonly=True, store=True)
    # ov_Reference = fields.Reference(string='Original Value', readonly=True, store=True)
    # ov_Many2one = fields.Many2one(string='Original Value', readonly=True, store=True)
    # ov_One2many = fields.One2many(string='Original Value', readonly=True, store=True)
    # ov_Many2many = fields.Many2many(string='Original Value', readonly=True, store=True)

    nv_Boolean = fields.Boolean(string='New Value', track_visibility='onchange')
    nv_Integer = fields.Integer(string='New Value', track_visibility='onchange')
    nv_Float = fields.Float(string='New Value', track_visibility='onchange')
    # nv_Monetary = fields.Monetary(string='New Value', track_visibility='onchange')
    nv_Char = fields.Char(string='New Value', track_visibility='onchange')
    nv_Text = fields.Text(string='New Value', track_visibility='onchange')
    nv_Html = fields.Html(string='New Value', track_visibility='onchange')
    nv_Date = fields.Date(string='New Value', track_visibility='onchange')
    nv_Datetime = fields.Datetime(string='New Value', track_visibility='onchange')
    nv_Binary = fields.Binary(string='New Value', track_visibility='onchange')

    # nv_Selection = fields.Selection(selection=[('','')], string='')
    # nv_Reference = fields.Reference(string='')
    # nv_Many2one = fields.Many2one(string='')
    # nv_One2many = fields.One2many(string='')
    # nv_Many2many = fields.Many2many(string='')

    # is_invisible = fields.Boolean(string='invisible mark', compute='_compute_is_invisible')

    @api.onchange('fields_id')
    def set_fields_string(self):
        if not self.fields_id or not self.updbs_model_id.model_record:
            return
        field_name = self.fields_id.name
        model_obj = self.updbs_model_id.model_record
        field_value = getattr(model_obj, field_name, False)
        ttype = self.fields_type.title()
        ov_field_name = 'ov_' + ttype

        # field_name ttype  relation_field
        dict(model_obj.fields_get(field_name)[field_name][ttype])

        setattr(self, ov_field_name, field_value)


class UpdbsDataModel(models.Model):
    _name = 'updbs.model'
    _description = 'Updbs Data Model'
    _inherit = ['mail.thread']
    _rec_name = 'name'

    def _init_model_record(self):
        model_records = self.sudo().env['ir.model'].search([('transient', '=', False)], order='name asc')
        result = [(r.model, r.name) for r in model_records]
        return result

    name = fields.Char(string='Name', required=True, store=True, compute='_compute_name', track_visibility='onchange')
    model_id = fields.Many2one('ir.model', string='Model', required=True, track_visibility='onchange')
    model_record = fields.Reference(selection=_init_model_record, string='Record', required=True, track_visibility='onchange')
    fields_data_ids = fields.One2many('updbs.model.line', 'updbs_model_id', string='fields Data', track_visibility='onchange')
    notify = fields.Char(string='Notify', readonly=True, track_visibility='onchange')

    def _compute_name(self):
        for record in self:
            name = f'{record.model_id.name}--{record.model_record.display_name}'
            record.name = name

    @api.onchange('model_id')
    def _onchange_model_id(self):
        self.model_record = False
        if not self.model_id:
            return
        self.model_record = self.env[self.model_id.model].search([], order='id desc', limit=1)

    @api.onchange('model_record')
    def _onchange_model_record(self):
        self.model_id = False
        if not self.model_record:
            return
        self.model_id = self.env['ir.model'].search([('model', '=', self.model_record._name)])
